package com.cike.productmanager.DBHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cike.productmanager.DBHelper.DBHelper.TABLE;
import com.cike.productmanager.DBHelper.TablesColumns.ActivityColumns;
import com.cike.productmanager.DBHelper.TablesColumns.ActivityFeedbackColumns;
import com.cike.productmanager.DBHelper.TablesColumns.ContractColumns;
import com.cike.productmanager.DBHelper.TablesColumns.ContractFeedbackColumns;
import com.cike.productmanager.DBHelper.TablesColumns.DisplayGuideColumns;
import com.cike.productmanager.DBHelper.TablesColumns.StoreColumns;

public class DBManager implements IBaseServer{

	private static DBHelper mDbHelper;
	private static SQLiteDatabase db;
	
	//决定对哪个数据表进行操作
	private static final int TYPE_USER =              1;
	private static final int TYPE_ACTIVITY =          2;
	private static final int TYPE_ACTIVITY_FEEDBACK = 3;
	private static final int TYPE_CONTRACT =          4;
	private static final int TYPE_CONTRACT_FEEDBACK = 5;
	private static final int TYPE_STORE =             6;
	private static final int TYPE_CUSTOMER =          7;
	private static final int TYPE_ROLE =              8;
	private static final int TYPE_PERMISSION =        9;
	private static final int TYPE_MARKET_VISIT =      10;
	private static final int TYPE_DISPLAY_GUIDE =     11;
	private static final int TYPE_VISIT =             12;
	
	//对数据表操作的关键字数组
	private static final String[] OPERATION_COLUMNS_USER = 
		{};
	
	private static final String[] OPERATION_COLUMNS_ACTIVITY = 
		{ActivityColumns.START_TIME, ActivityColumns.END_TIME, ActivityColumns.DISPLAY_GUIDE_ID,
		ActivityColumns.STORE_ID, ActivityColumns.ACTIVITY_NAME, ActivityColumns.SALER_NUMBER,
		ActivityColumns.GOAL, ActivityColumns.REQUIRE};
	
	private static final String[] OPERATION_COLUMNS_ACTIVITY_FEEDBACK = 
		{ActivityFeedbackColumns.MATERIAL_STATUS, ActivityFeedbackColumns.DISPLAY_STATUS,
		ActivityFeedbackColumns.PRODUCT_STATUS, ActivityFeedbackColumns.STATUS};
	
	private static final String[] OPERATION_COLUMNS_CONTRACT = 
		{ContractColumns.START_TIME,ContractColumns.END_TIME,ContractColumns.DISPLAY_GUIDE_ID,
		ContractColumns.STORE_ID,ContractColumns.CONTRACT_NAME,ContractColumns.TYPE,
		ContractColumns.CONTENT,ContractColumns.IMPORTANCE};
	
	private static final String[] OPERATION_COLUMNS_CONTRACT_FEEDBACK = 
		{ContractFeedbackColumns.STATUS};
	
	private static final String[] OPERATION_COLUMNS_STORE = 
		{StoreColumns.STORE_NAME,StoreColumns.PHONE,StoreColumns.PROVINCE,
		 StoreColumns.CITY,StoreColumns.DISTRICT,StoreColumns.STREET,
		 StoreColumns.STORE_TYPE};
	
	private static final String[] OPERATION_COLUMNS_CUSTOMER = {};
	
	private static final String[] OPERATION_COLUMNS_ROLE = {};
	
	private static final String[] OPERATION_COLUMNS_PERMISSION = {};
	
	private static final String[] OPERATION_COLUMNS_MARKET_VISIT = {};
	
	private static final String[] OPERATION_COLUMNS_DISPLAY_GUIDE = 
		{DisplayGuideColumns.CONTENT};
	
	private static final String[] OPERATION_COLUMNS_VISIT = {};
	
	
	public DBManager(Context context) {
		super();
	}
	
	/**
	 * open database
	 */
	public static Boolean openDB(Context context){
		mDbHelper = DBHelper.getInstance(context);
		db = mDbHelper.getReadableDatabase();
	    if (db != null) {
	    	return true;
		}
		return false;
	}
	
	/**
	 * close database
	 */
	public static void closeDB(){
		if (db != null) {
			db.close();
		}
		mDbHelper = null;
	}
	
	/**
	 * delete all the tables
	 */
	public static void deleteAllTables(){
		db.execSQL("DROP TABLE IF EXISTS "+ TABLE.ACTIVITY);
		db.execSQL("DROP TABLE IF EXISTS "+ TABLE.CONTRACT);
		db.execSQL("DROP TABLE IF EXISTS "+ TABLE.DISPLAY_GUIDE);
		db.execSQL("DROP TABLE IF EXISTS "+ TABLE.VISIT);
		db.execSQL("DROP TABLE IF EXISTS "+ TABLE.USER);
		db.execSQL("DROP TABLE IF EXISTS "+ TABLE.STORE);
	}
	
	/**
	 * 下面是数据库的一些基本操作，具体操作将在实体的成员函数中实现
	 */
	/**
	 * Convenience method for query rows in the database.
     *@param  type  The table name to compile the query against.
     *@param projection  A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used. 
     *@param selection  A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table.
     *@param  selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings. 
     *@param sortOrder How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered
     *@return A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized, see the documentation for more details.
     *@author CaoRen
	 */
	public Cursor query(int type, String[] projection, String selection, String[] selectionArgs,
            String sortOrder, Context context){
		Cursor cursor = null;
		if (db == null) {
			openDB(context);
		}
		switch (type) {
		case TYPE_USER:
			cursor = db.query(TABLE.USER, projection, selection, selectionArgs, null, null,
                    sortOrder);
			break;
        case TYPE_ACTIVITY:
        	cursor = db.query(TABLE.ACTIVITY, projection, selection, selectionArgs, null, null,
                    sortOrder);
			break;
        case TYPE_CONTRACT:
        	cursor = db.query(TABLE.CONTRACT, projection, selection, selectionArgs, null, null,
                    sortOrder);
	        break;
        case TYPE_STORE:
        	cursor = db.query(TABLE.STORE, projection, selection, selectionArgs, null, null,
                    sortOrder);
	        break;
        case TYPE_VISIT:
        	cursor = db.query(TABLE.VISIT, projection, selection, selectionArgs, null, null,
                    sortOrder);
	        break;
        case TYPE_DISPLAY_GUIDE:
        	cursor = db.query(TABLE.DISPLAY_GUIDE, projection, selection, selectionArgs, null, null,
                    sortOrder);
	        break;
		default:
			throw new IllegalArgumentException("Unknown TYPE "+type);
		}
		
		return cursor;
	}
	
	/**
	 * Convenience method for insert rows in the database.
     *@param  type  the table to insert the row into
     *@param contentValues  this map contains the initial column values for the row. The keys should be the column names and the values the column values 
     *@param selection  the optional WHERE clause to apply when deleting.
     *@param  selectionArgs Passing null will update all rows. 
     *@return the number of rows affected 
	 *@author CaoRen
	 */
	public long insert(int type, ContentValues contentValues,Context context){
		long insertedId = 0;
		if (db == null) {
			openDB(context);
		}
		switch (type) {
		case TYPE_USER:
			insertedId = db.insert(TABLE.USER, null, contentValues);
			break;
        case TYPE_ACTIVITY:
        	insertedId = db.insert(TABLE.ACTIVITY, null, contentValues);
			break;
        case TYPE_CONTRACT:
        	insertedId = db.insert(TABLE.CONTRACT, null, contentValues);
	        break;
        case TYPE_STORE:
        	insertedId = db.insert(TABLE.STORE, null, contentValues);
	        break;
        case TYPE_VISIT:
        	insertedId = db.insert(TABLE.VISIT, null, contentValues);
	        break;
        case TYPE_DISPLAY_GUIDE:
        	insertedId = db.insert(TABLE.DISPLAY_GUIDE, null, contentValues);
	        break;
		default:
			throw new IllegalArgumentException("Unknown TYPE "+type);
		}
		
		return insertedId;
	}
	
	/**
	 * Convenience method for delete rows in the database.
     *@param  type  the table to delete from
     *@param contentValues  a map from column names to new column values. null is a valid value that will be translated to NULL. 
     *@param selection  the optional WHERE clause to apply when deleting.
     *@param  selectionArgs Passing null will update all rows. 
     *@return the number of rows affected 
	 *@author CaoRen
	 */
	public int delete(int type, String selection, String[] selectionArgs, Context context){
		db = mDbHelper.getWritableDatabase();
		int deleteId = 0;
		switch (type) {
		case TYPE_USER:
			deleteId = db.delete(TABLE.USER, selection, selectionArgs);
			break;
        case TYPE_ACTIVITY:
        	deleteId = db.delete(TABLE.ACTIVITY, selection, selectionArgs);
			break;
        case TYPE_CONTRACT:
        	deleteId = db.delete(TABLE.CONTRACT, selection, selectionArgs);
	        break;
        case TYPE_STORE:
        	deleteId = db.delete(TABLE.STORE, selection, selectionArgs);
	        break;
        case TYPE_VISIT:
        	deleteId = db.delete(TABLE.VISIT, selection, selectionArgs);
	        break;
        case TYPE_DISPLAY_GUIDE:
        	deleteId = db.delete(TABLE.DISPLAY_GUIDE, selection, selectionArgs);
	        break;
		default:
			throw new IllegalArgumentException("Unknown TYPE "+type);
		}
		
		return deleteId;
		
	}
	/**
	 * Convenience method for updating rows in the database.
     *@param  type  the table to update in
     *@param contentValues  a map from column names to new column values. null is a valid value that will be translated to NULL. 
     *@param selection  the optional WHERE clause to apply when updating. 
     *@param  selectionArgs Passing null will update all rows. 
     *@return the number of rows affected 
	 *@author CaoRen
	 */
	public int update(int type, ContentValues contentValues, String selection, String[] selectionArgs, Context context){
		db = mDbHelper.getWritableDatabase();
		int updateId = 0;
		switch (type) {
		case TYPE_USER:
			updateId = db.update(TABLE.USER, contentValues, selection, selectionArgs);
			break;
        case TYPE_ACTIVITY:
        	updateId = db.update(TABLE.ACTIVITY, contentValues, selection, selectionArgs);
			break;
        case TYPE_CONTRACT:
        	updateId = db.update(TABLE.CONTRACT, contentValues, selection, selectionArgs);
	        break;
        case TYPE_STORE:
        	updateId = db.update(TABLE.STORE, contentValues, selection, selectionArgs);
	        break;
        case TYPE_VISIT:
        	updateId = db.update(TABLE.VISIT, contentValues, selection, selectionArgs);
	        break;
        case TYPE_DISPLAY_GUIDE:
        	updateId = db.update(TABLE.DISPLAY_GUIDE, contentValues, selection, selectionArgs);
	        break;
		default:
			throw new IllegalArgumentException("Unknown TYPE "+type);
		}
		
		return updateId;
		
	}

	@Override
	public Object getBaseInfo(int entityType, String[] projection, String selection, String[] selectionArgs,
            String sortOrder, Context context) {
		Cursor cursor = null;
		cursor = query(entityType, projection, selection, selectionArgs, sortOrder, context);
		
		
		return null;
	}

	@Override
	public boolean deleteRecord(int entityType) {
		// TODO 自动生成的方法存根
		return false;
	}

	@Override
	public boolean updateBaseInfo(int entityType) {
		// TODO 自动生成的方法存根
		return false;
	}

	@Override
	public int insertRecord(int entityType) {
		// TODO 自动生成的方法存根
		return 0;
	}
	
	
	
}
